from django.db import models
from django.contrib.auth.models import User

TORRENT_STATUS = (
    ('A','Active'),  # Torrent is in the client.
    ('D','Deleted'), # Torrent is not in the client.
)

class Torrent(models.Model):
    hash = models.CharField("Torrent Infohash",max_length=40)
    user_id = models.ForeignKey(User)
    name = models.CharField("Torrent Name",max_length=255)
    simplename = models.CharField("Simplified Torrent Name",max_length=255)
    file = models.CharField("Torrent File Path",max_length=255)
    status = models.CharField("Torrent State",max_length=10,choices=TORRENT_STATUS,default="A")
    bandwidth = models.CharField("Torrent Bandwidth", max_length=20,default=0,blank=True)
    creation = models.DateTimeField(auto_now_add=True)
    modified = models.DateTimeField(auto_now=True)
    deleted = models.DateTimeField(blank=True,null=True)
    
    def __unicode__(self):
        return self.name
        
class Comment(models.Model):
    torrent_id = models.ForeignKey(Torrent)
    user_id = models.ForeignKey(User)
    comment = models.CharField("Comment",max_length=255)
    creation = models.DateTimeField(auto_now_add=True)
    modified = models.DateTimeField(auto_now=True)

    def __unicode__(self):
        if(len(str(self.comment)) > 18):
            return str(self.user_id) + ': '+ str(self.comment[0:15] + '...')
        else:
            return str(self.user_id) + ': '+ str(self.comment[0:18])
            
class OrderedComment(Comment):
    class Meta:
        ordering = ["-creation"]
        proxy = True
        
class Log(models.Model):
    torrent_id = models.ForeignKey(Torrent)
    user_id = models.ForeignKey(User)
    ip = models.CharField("IP Address",max_length=15)
    action = models.CharField("Logged Event",max_length=255)
    creation = models.DateTimeField(auto_now_add=True)
    modified = models.DateTimeField(auto_now=True)
    
    def __unicode__(self):
        if(len(str(self.action)) > 30):
            return str(self.action)[0:27]+'...'
        else:
            return str(self.action)

class OrderedLog(Log):
	class Meta:
		ordering = ["-creation"]
		proxy = True
    
        